/**
 * v-auth
 * 按钮权限指令
 */
import { useAuthStore } from "@/stores/modules/auth";
import type { Directive, DirectiveBinding } from "vue";

const auth: Directive = {
  beforeMount(el, binding, vnode, prevVnode) {
   // console.log( el.style.display)
    el.style.opacity='0'
  },
  mounted(el: HTMLElement, binding: DirectiveBinding) {

    // const { value } = binding;

    // console.log('ssssssssssssssssssssssss')
    // console.log(value)


    // const authStore = useAuthStore();
    // const currentPageRoles = authStore.authButtonListGet[authStore.routeName] ?? [];
    // if (value instanceof Array && value.length) {
    //   const hasPermission = value.every(item => currentPageRoles.includes(item));
    //   if (!hasPermission) el.remove();
    // } else {
    //   if (!currentPageRoles.includes(value)) el.remove();
    // }



   
    const { value } = binding;
    const getkey=value.key;
    const authArr=value.authArr;
    for (var keyp in authArr) {
      if(keyp==getkey){
        if(authArr[keyp].show){
          // console.log('sdffffffffff')
          // console.log(authArr[keyp])

          //el.style.display='inline-block'
          el.style.opacity='1'
          if(authArr[keyp].action){
              el.setAttribute('disabled', 'disabled');
              el.classList.add('is-disabled');    
          }
        }else{
          el.remove();
        }
      }

    }





  },
  updated(el, binding, vnode, prevVnode) {
 
  
    const { value } = binding;
    // console.log(value)
    const getkey=value.key;
    const authArr=value.authArr;
    for (var keyp in authArr) {
      if(keyp==getkey){
        if(authArr[keyp].show){
          el.style.opacity='1'
          if(authArr[keyp].action==false){
            el.setAttribute('disabled', 'disabled');
            el.classList.add('is-disabled');   
          }
        }else{
          el.remove();
        }
      }

    }
  },

};

export default auth;
